package com.weatherquery.weatherapi;

import com.weatherquery.dao.WeatherDAO;
import com.weatherquery.informbean.WeatherData;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author Natural-Pride
 * @Date 2023/12/19 14:38
 * @注释 调用百度天气API将城市未来五天天气预报信息插入insertForecastWeather()
 */
public class WeatherDataAPI {
    public static void main(String[] args) throws Exception {
        //创建 CityCodeMap对象,用于获取所有的城市代码
        cityCodeMap map = new cityCodeMap();
        //获取所有的城市代码
        String[] cityCodes = map.getAllCityCodes();
        //遍历城市代码数组,并对每个城市代码调用 Weather_Query方法
        for (String cityCode : cityCodes) {
            insertForecastWeather(cityCode);
        }
    }

    private static void insertForecastWeather(String id) throws IOException {
        URL url = new URL("https://api.map.baidu.com/weather/v1/?" +
                "district_id=" + id + "&data_type=all&ak=m9qYwncmWOdvxrU0U62q5D1oEBp4Mrsq");
        InputStream is = url.openStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();
        try {
            // 解析JSON返回结果
            JSONObject jsonObject = JSONObject.fromObject(line);
            // 创建Weather_DAO对象
            JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
            String cityName = location.getString("city");
            String cityId = location.getString("id");
            JSONObject now = jsonObject.getJSONObject("result").getJSONObject("now");
            String weather = now.getString("text");
            String temperature = now.getString("temp");
            String windClass = now.getString("wind_class");
            String windDir = now.getString("wind_dir");
            String updateTime = now.getString("uptime");
            // 时间戳转换
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
            Date date=sdf.parse(updateTime);
            SimpleDateFormat outputFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formatted=outputFormat.format(date);
            // 未来五天天气数据
            JSONArray forecasts = jsonObject.getJSONObject("result").getJSONArray("forecasts");
            String forecastDate = null;
            String forecastWeek = null;
            String forecastWeatherDay = null;
            String forecastTempHigh = null;
            String forecastTempLow = null;
            WeatherDAO weatherDAO = null;
            WeatherData weatherData= null;

            for (Object forecast : forecasts) {
                jsonObject = (JSONObject) forecast;
                forecastDate = jsonObject.getString("date");
                forecastWeek = jsonObject.getString("week");
                forecastWeatherDay = jsonObject.getString("text_day");
                forecastTempHigh = jsonObject.getString("high");
                forecastTempLow = jsonObject.getString("low");

                // 创建Weather_DAO对象
                weatherData = new WeatherData(cityId,cityName, weather, temperature, windClass, windDir, formatted,
                        forecastDate, forecastWeek, forecastWeatherDay, forecastTempHigh, forecastTempLow);
                weatherDAO = new WeatherDAO();
                // 存储天气数据
                weatherDAO.insertWeatherData(weatherData);
            }
        } catch (Exception e) {
           e.printStackTrace();
        }
    }
}
